// pages/index/register/register.js
const MD5=require("../js/MD5")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    password:"",
    confirmpassword:"",
    registerDialog:true,
    state:"",
    status:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.dialog = this.selectComponent("#dialog");
  },
  userInput: function (e) {
    this.setData({
      phone:e.detail.value
   })  
   
  },
  passInput: function (e) {
    this.setData({
      password:e.detail.value
   })  
  },
  confirmpassword:function(e){
    this.setData({
      confirmpassword:e.detail.value,
   })  
  },
  _success() {
    this.dialog.hidedialog();
    this.setData({
      registerDialog:true,
    })
  },
  register: function (e) {
    let that= this
let phone= that.data.phone
let password= that.data.password
let confirmpassword= that.data.confirmpassword
if (password!=""&&confirmpassword!=""&&phone!="") {
  if((/^1[3456789]\d{9}$/.test(phone))){ 
    this.dialog.showdialog();
    this.setData({
      registerDialog:false,
      state:"手机号码有误，请重填"
    })
} else{
  if (password==confirmpassword) {
    wx.request({
      url: 'http://vps.wdf2020.cf:8888/user/login',
      method:"POST",
     data:{
       username:that.data.phone,
       password:password,
     },
     success (res) {
      console.log(res)
      that.data.status=res.data.code
      wx.setStorage({
        data: res.cookies[0],
        key: 'sessionid',
      })
      wx.setStorage({//授权
        data: res.header.Authorization,
        key: 'session',
      })
      

       //判断是否已被注册
    if (that.data.status==200) {
      that.dialog.showdialog();
      that.setData({
        registerDialog:false,
        state:"恭喜你，注册成功"
      })
    } else {
      that.dialog.showdialog();
      that.setData({
        registerDialog:false,
        state:"账号已被注册"
      })
    }
    }
    })
   
  
    // wx.switchTab({
    //   url:"/pages/logs/logs"
    // })
    setTimeout(function () {
      wx.switchTab({
        url:"/pages/logs/logs"
      })
    }, 1500)
  } else {
    this.dialog.showdialog();
    this.setData({
      registerDialog:false,
      state:"两次密码输入不一致"
    })
  }
}

} else {
  this.dialog.showdialog();
  this.setData({
    registerDialog:false,
    state:"账号密码不能为空"
  })
}


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})